'use strict';

const Controller = require('egg').Controller;
const moment = require('moment')

class ArticleController extends Controller {
    // 发布文章
    async create() {
        const { ctx } = this;
        const data = {
            ...ctx.request.body,
            createTime: moment().format('YYYY-MM-DD HH:mm:ss')
        };
        const result = await ctx.service.article.create(data);
        if (result) {
            ctx.body = {
                success: true,
                status: 200,
                data: result,
                message: ''
            }
        } else {
            ctx.body = {
                success: false,
                status: 500,
                data: null,
                message: '发布文章失败'
            }
        }
    }

    // 文章列表
    async lists() {
        const { ctx } = this;
        const result = await ctx.service.article.lists();
        if (result) {
            ctx.body = {
                success: true,
                status: 200,
                data: result,
                message: ''
            }
        } else {
            ctx.body = {
                success: false,
                status: 500,
                data: null,
                message: '获取文章列表失败'
            }
        }
    }
    async details() {
        const { ctx } = this;
        const result = await ctx.service.article.details(ctx.query.id)
        if (result) {
            ctx.body = {
                success: true,
                status: 200,
                data: result,
                message: ''
            }
        } else {
            ctx.body = {
                success: false,
                status: 500,
                data: null,
                message: '获取文章详情失败'
            }
        }
    }
}


module.exports = ArticleController;
